﻿@model ProductReviewListModel
@using Telerik.Web.Mvc.UI
@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    
    //page title
    ViewBag.Title = T("Admin.Catalog.ProductReviews").Text;
}
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-catalog.png")" alt="" />
        @T("Admin.Catalog.ProductReviews")
    </div>
    <div class="options">
    <input type="button" id="approve-selected" class="t-button" value="@T("Admin.Catalog.ProductReviews.ApproveSelected")" />
    <input type="button" id="disapprove-selected" class="t-button" value="@T("Admin.Catalog.ProductReviews.DisapproveSelected")" />
    </div>
</div>
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CreatedOnFrom):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CreatedOnFrom)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CreatedOnTo):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => Model.CreatedOnTo)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchText):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => Model.SearchText)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="btnSearch" class="t-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
<table class="adminContent">
    <tr>
        <td>
            @(Html.Telerik().Grid<ProductReviewModel>()
                    .Name("productreviews-grid")
                            .ClientEvents(events => events.OnDataBinding("onDataBinding").OnDataBound("onDataBound"))
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.Id)
                        .ClientTemplate("<input type='checkbox' value='<#= Id #>' class='checkboxGroups'/>")
                        .Title("<input id='mastercheckbox' type='checkbox'/>")
                        .Width(50)
                        .HtmlAttributes(new { style = "text-align:center" })
                        .HeaderHtmlAttributes(new { style = "text-align:center" });
                        columns.Bound(x => x.ProductName)
                            .Width(200)
                            .Template(x => Html.ActionLink(x.ProductName, "Edit", "Product", new { id = x.ProductId }, new { }))
                            .ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Product/Edit/") + "<#= ProductId #>\"><#= ProductName #></a>");
                        columns.Bound(x => x.CustomerId)
                            .Width(100)
                            .Template(x => Html.ActionLink(x.CustomerInfo, "Edit", "Customer", new { id = x.CustomerId }, new { }))
                            .ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Customer/Edit/") + "<#= CustomerId #>\"><#= CustomerInfo #></a>");
                        columns.Bound(x => x.Title)
                            .Width(150)
                            .Centered();
                        columns.Bound(x => x.ReviewText)
                            .Width(350)
                            .Encoded(false)
                            .Centered();
                        columns.Bound(x => x.Rating)
                            .Width(50)
                            .Centered();
                        columns.Bound(x => x.IsApproved)
                            .Template(
                                @<img alt="" src="@Url.Content("~/Administration/Content/images/active-" + item.IsApproved.ToString().ToLowerInvariant() + ".gif")" />
                            )
                            .ClientTemplate("<img alt='' src='" + Url.Content("~/Administration/Content/images/") + "active-<#= IsApproved #>.gif' />")
                            .Centered()
                            .Width(100);
                        columns.Bound(x => x.CreatedOn)
                            .Width(100)
                            .Centered();
                        columns.Bound(x => x.Id)
                            .Width(50)
                            .Centered()
                            .Template(x => Html.ActionLink(T("Admin.Common.Edit").Text, "Edit", new { id = x.Id }))
                            .ClientTemplate("<a href=\"Edit/<#= Id #>\">" + T("Admin.Common.Edit").Text + "</a>")
                            .Title(T("Admin.Common.Edit").Text);
                    })
                                                .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                                                .DataBinding(dataBinding => dataBinding.Ajax().Select("List", "ProductReview"))
                                                .EnableCustomBinding(true))
        </td>
    </tr>
</table>

    <script type="text/javascript">

        var selectedIds = [];

        $(document).ready(function () {
        
            //search button
            $('#btnSearch').click(function () {
                //search
                var grid = $('#productreviews-grid').data('tGrid');
                grid.currentPage = 1; //new search. Set page size to 1
                grid.ajaxRequest();
                //clear selected checkboxes
                $('.checkboxGroups').attr('checked', false).change();
                selectedIds = [];
                return false;
            });
            
            //"approve selected" button
            $('#approve-selected').click(function (e) {
                e.preventDefault();

                var postData = {
                    selectedIds: selectedIds
                };

                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("ApproveSelected", "ProductReview"))",
                    data: postData,
                    complete: function (data) {
                        //reload grid
                        var grid = $('#productreviews-grid').data('tGrid');
                        //grid.currentPage = 1; //new search. Set page size to 1
                        grid.ajaxRequest();
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert(thrownError);
                    },
                    traditional: true
                });
                return false;
            });
            
            //"disapprove selected" button
            $('#disapprove-selected').click(function (e) {
                e.preventDefault();

                var postData = {
                    selectedIds: selectedIds
                };

                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("DisapproveSelected", "ProductReview"))",
                    data: postData,
                    complete: function (data) {
                        //reload grid
                        var grid = $('#productreviews-grid').data('tGrid');
                        //grid.currentPage = 1; //new search. Set page size to 1
                        grid.ajaxRequest();
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert(thrownError);
                    },
                    traditional: true
                });
                return false;
            });

            
            $('#mastercheckbox').click(function () {
                $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
            });

            //wire up checkboxes. 
            $('#productreviews-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                var $check = $(this);
                if ($check.is(":checked") == true) {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked == -1) {
                        //add id to selectedIds.  
                        selectedIds.push($check.val());
                    }
                }
                else {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked > -1) {
                        //remove id from selectedIds.  
                        selectedIds = $.grep(selectedIds, function (item, index) {
                            return item != $check.val();
                        });
                    }
                }
                updateMasterCheckbox();
            });
        });

        function onDataBinding(e) {
            var searchModel = {
                CreatedOnFrom: $('#@Html.FieldIdFor(model => model.CreatedOnFrom)').val(),
                CreatedOnTo: $('#@Html.FieldIdFor(model => model.CreatedOnTo)').val(),
                SearchText: $('#@Html.FieldIdFor(model => model.SearchText)').val()
            };
            e.data = searchModel;
        }

        function onDataBound(e) {

            $('#productreviews-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                var currentId = $(this).val();
                var checked = jQuery.inArray(currentId, selectedIds);
                //set checked based on if current checkbox's value is in selectedIds.  
                $(this).attr('checked', checked > -1);
            });

            updateMasterCheckbox();
        }
        
        function updateMasterCheckbox() {
            var numChkBoxes = $('#productreviews-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#productreviews-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
            $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }

    </script>
